/*
 * @Descripttion: 
 * @version: 
 * @Author: Andy
 * @Date: 2020-09-18 16:19:58
 * @LastEditors: Andy
 * @LastEditTime: 2020-09-24 16:19:07
 */
import { UI_Listen } from '../../config/ui_def'
import { getGachaid } from '../tool/gacha';

cc.Class({
    extends: cc.Component,

    properties: {
        spineBG: sp.Skeleton,
        // roleBG: cc.Sprite,
        equipGrp: cc.Node,
        lock: cc.Node,
        mainEquip: cc.Node,
        btn_sp: cc.Sprite,
        lockBtn: cc.Node,
        detailBtn: cc.Node,
        changBtn: cc.Node,
        chooseFrame: cc.Node,
        equipBtn_sp: cc.Sprite,
    },
    // update (dt) {},
    init(info, msg) {
        let posGrp = [[-140, 0], [0, -140], [140, 0], [0, 140]]
        let addNum = 4 - this.equipGrp.childrenCount
        let reward_prefab = he.load.get("uimod/equipItem")
        for (let i = 0; i < addNum; i++) {
            let reward_node = cc.instantiate(reward_prefab)
            this.equipGrp.addChild(reward_node)
            this.equipGrp.children[i].setPosition(posGrp[i][0], posGrp[i][1])
        }

        this.equipGrpTween = null
        this.equipChildTween = []
        this.stopAllTween()
        this.isRotate = false
        if (info) {
            // this.roleBG.spriteFrame = he.load.get('overPage', cc.SpriteAtlas, 'plist').getSpriteFrame(`pose_${info}`)
            this.mE_title_lab = this.mainEquip.getChildByName('title').getComponent(cc.Label)
            this.mE_skill_lab = this.mainEquip.getChildByName('skill').getComponent(cc.Label)
            this.mE_detail_lab = this.mainEquip.getChildByName('detail').getComponent(cc.Label)
            let name = null
            switch (info) {
                case 1001:
                    name = 'blue';
                    break
                case 1002:
                    name = 'red';
                    break
                case 1003:
                    name = 'pink';
                    break
                case 1004:
                    name = 'yellow'
                    break
                case 1005:
                    name = 'black'
                    break
            }
            // spineBG.
            this.spineBG.defaultSkin = `${name}`
            this.spineBG.setSkin(`${name}`)
            let passLen = he.local.getJson('pass_list', 'user').length
            let isLock = true

            isLock = !this.checkInTeam(info)
            if (isLock) {
                this.chooseFrame.active = false
                this.mainEquip.active = false
                this.equipGrp.active = false
                this.changBtn.active = false
                this.lock.active = true
                this.detailBtn.active = false
                this.spineBG.setAnimation(0, `stand_by2`, true)
                if (info == 1002) {
                    this.lockBtn.active = true
                    this.detailBtn.acttive = false
                } else {
                    this.lockBtn.active = false
                    this.detailBtn.acttive = true
                }
            } else {
                this.changBtn.active = true
                this.chooseFrame.active = true
                this.spineBG.setAnimation(0, `stand_by`, true)
                this.lockBtn.active = false
                this.detailBtn.active = true
                this.mainEquip.active = true
                let roleTeam = he.local.getJson('team', 'user')
                for (let i = 0; i < roleTeam.length; i++) {
                    if (roleTeam[i].id == info) {
                        this.info = roleTeam[i]
                        break
                    }
                }
                this.hand = this.info.hand
                // this.chooseIndex = this.hand
                this.initEquipPos()
                let equipInfo = he.db.getDataByArgs('Equip', { equip: this.hand, level: this.info.equips[this.hand] })
                this.mE_title_lab.string = `${equipInfo.name} lv.${equipInfo.level}`
                this.mE_skill_lab.string = `${equipInfo.text}`
                this.mE_detail_lab.string = `+${equipInfo.attack} 攻击伤害\n+${equipInfo.attackSpeed} 攻击速度`
                this.equipGrp.active = true
                this.lock.active = false
                this.equipGrpInit(this.info.equips, this.info.hand)
            }
            this.changeEquipLockSp()
            // gg.eventManager.on('getBtnClick', this.changeBtnSp, this)
            // gg.eventManager.on('infoUpdate', this.infoUpdateServer, this)
            gg.eventManager.on('equipItem', this.listenClient, this)
        }
    },
    listenClient(param) {
        switch (param.code) {
            case UI_Listen.EQUIPITEM_GETCLICK:
                this.changeBtnSp(param.state)
                break
            case UI_Listen.EQUIPITEM_INFOUPDATE:
                this.infoUpdateServer(param.info)
                break
            case UI_Listen.EQUIPITEM_ITEMCLICK:
                this.oneTimeToPos(param.index)
                break
        }
    },

    oneTimeToPos(index) {

        if (this.hand == undefined || this.hand == null) {
            return
        }
        if (index == this.hand) {
            return
        } else {
            let num = (this.hand - index)
            if (num) {
                // console.log(num)
                if (this.isRotate) {
                    return
                }
                if (num == 3) {
                    num = -1
                }
                this.isRotate = true
                this.equipGrpTween = cc.tween(this.equipGrp)
                    .call(() => {
                        this.hand = index
                    })
                    .by(0.3, { angle: 90 * num })
                    .call(() => {
                        this.isRotate = false
                        this.updateME()
                    })
                    .start()
                for (let i = 0; i < 4; i++) {
                    this.equipChildTween[i] = cc.tween(this.equipGrp.children[i])
                        .by(0.3, { angle: -90 * num })
                        .start()
                }
                this.changeEquipLockSp(true, index)
            }

        }
    },

    //改按钮图片
    changeBtnSp(param) {
        switch (param) {
            case UI_Listen.EQUIP_NONE:
                this.btn_sp.node.parent.active = false
                // this.btn_sp.spriteFrame= he.load.get('equipPage', cc.SpriteAtlas, 'plist').getSpriteFrame("text_jiesuo")
                break
            case UI_Listen.EQUIP_SHANGCHANG:
                this.btn_sp.node.parent.active = true
                this.btn_sp.spriteFrame = he.load.get('rolePage', cc.SpriteAtlas, 'plist').getSpriteFrame("btn_attack")
                break
            case UI_Listen.EQUIP_TIHUAN:
                this.btn_sp.node.parent.active = true
                this.btn_sp.spriteFrame = he.load.get('rolePage', cc.SpriteAtlas, 'plist').getSpriteFrame("btn_attack")
                break
            case UI_Listen.EQUIP_CHEXIA:
                this.btn_sp.node.parent.active = true
                this.btn_sp.spriteFrame = he.load.get('rolePage', cc.SpriteAtlas, 'plist').getSpriteFrame("btn_rest")
                break
        }
        // this.btn_sp.node.setScale(0.8)
        // this.actTween = cc.tween(this.btn_sp.node)
        //     .to(0.2, { scale: 1 }, { easing: `elasticOut` })
        //     .start()
    },

    touchStartServer() {
        this.lockBtn.active = false
        this.btn_sp.node.parent.active = false
    },

    clickFunc() {
        gg.eventManager.emit('equip', { type: UI_Listen.EQUIP_CLCIKBTN, index: this.index })

    },

    lockFuncServer() {
        gg.eventManager.emit('equip', { type: UI_Listen.EQUIP_LOOKAD })
    },

    clear() {
        // gg.eventManager.off('getBtnClick', this.changeBtnSp, this)
        if (this.actTween) {
            this.actTween.stop()
            this.actTween = null
        }
    },

    checkInTeam(num) {
        let teamGrp = he.local.getJson('team', 'user')
        for (let i = 0; i < teamGrp.length; i++) {
            if (teamGrp[i].id == num) {
                return true
            }
        }
        return false
    },
    //角色的四个装备 更新
    equipGrpInit(grp, msg) {
        // console.log(grp)
        // console.log(msg)
        if (grp) {
            for (let i = 0; i < grp.length; i++) {
                //设置装备的信息
                this.equipGrp.children[i].getComponent('equipItem').infoInit(this.info, i)
                this.equipGrp.children[i].getComponent('equipItem').hideLight()
                //获取不是为空的第一个装备的index
            }
            if (msg >= 0) {
                this.equipGrp.children[msg].getComponent('equipItem').showLight()
                // this.equipGrp.children[msg].getComponent('equipItem').clickServer()
            }
        }
    },

    infoUpdateServer(info) {
        if (this.info) {
            if (info.id == this.info.id) {
                this.info = info
            }
            this.equipGrpInit(this.info.equips, this.info.hand)
            this.changeEquipLockSp()
            this.updateME()
        }
    },

    initEquipPos() {
        for (let i = 0; i < 4; i++) {
            this.equipGrp.children[i].angle = (90) * this.hand
        }
        this.equipGrp.angle = (-90) * this.hand
    },

    changBtnServer() {
        // console.log(this.hand)
        if (this.isRotate) {
            return
        }
        this.isRotate = true
        this.equipGrpTween = cc.tween(this.equipGrp)
            .call(() => {
                this.hand += 1
                if (this.hand >= 4) {
                    this.hand = 0
                }
            })
            .by(0.3, { angle: -90 })
            .call(() => {
                this.isRotate = false
                this.updateME()
            })
            .start()
        for (let i = 0; i < 4; i++) {
            this.equipChildTween[i] = cc.tween(this.equipGrp.children[i])
                .by(0.3, { angle: 90 })
                .start()
        }
        this.changeEquipLockSp(true)
    },

    updateME() {
        let equipInfo = he.db.getDataByArgs('Equip', { equip: this.hand, level: this.info.equips[this.hand] })
        if (equipInfo) {
            this.mainEquip.active = true
            this.lock.active = false
            this.mE_title_lab.string = `${equipInfo.name} lv.${equipInfo.level}`
            this.mE_skill_lab.string = `${equipInfo.text}`
            this.mE_detail_lab.string = `+${equipInfo.attack} 攻击伤害\n+${equipInfo.attackSpeed} 攻击速度`
        } else {
            this.mainEquip.active = false
            this.lock.active = true
        }
    },

    zenqiangServer() {
        gg.eventManager.emit('equip', { type: UI_Listen.EQUIP_CHOOSE, info: this.info, index: this.hand })
    },


    stopAllTween() {
        if (this.equipGrpTween) {
            this.equipGrpTween.stop()
            this.equipGrpTween = null
        }

        for (let i = 0; i < 4; i++) {
            if (this.equipChildTween) {
                if (this.equipChildTween[i]) {
                    this.equipChildTween[i].stop()
                    this.equipChildTween[i] = null
                }
            }

        }
    },

    changeEquipLockSp(param, zindex) {
        if (this.info) {
            let index = this.hand
            if (zindex >= 0) {
                index = zindex
            } else {
                if (param) {
                    index += 1
                    if (index >= 4) {
                        index = 0
                    }
                }
            }


            // console.log(this.info.equips[index])
            if (!this.info.equips[index]) {
                this.equipBtn_sp.spriteFrame = he.load.get('rolePage', cc.SpriteAtlas, 'plist').getSpriteFrame("btn_unlock")
            } else {
                this.equipBtn_sp.spriteFrame = he.load.get('rolePage', cc.SpriteAtlas, 'plist').getSpriteFrame("btn_strengthen")
            }
        }
    },
});
